package cn.edu.swu.dlx.utils;

import cn.edu.swu.dlx.entity.Book;

import java.util.List;

public class HtmlHelper {

    public static String wrapHtml(String content) {
        StringBuilder builder = new StringBuilder();
        String template= """
                <html>
                    <head>
                        <meta charset="UTF-8">
                        <title>西大网上书城</title>
                        <link rel="stylesheet" href="css/bookstore.css" />
                    </head>
                    <body>
                    <center>
                        <h1>欢迎来到西大网上书城</h1>
                        <a href="./add_book.html">添加新图书</a>&nbsp;&nbsp;&nbsp;&nbsp;
                        <a href="./books">查看图书列表</a>
                        <br>
        <form action="./searchBook" method="get">
            <input type="text" name="content"> &nbsp;&nbsp;&nbsp; <input type="submit" value="查询">
        </form>
                        <br>
                         %s
                    </center>
                    </body>
                </html>
""";
        builder.append(String.format(template, content));
        return builder.toString();
    }

    public static String buildBooksTable(List<Book> books) {
        StringBuilder sb = new StringBuilder();
        sb.append("<table class='tb-book'>");
        sb.append("<tr>")
                .append("<th>编号</th><th>名字</th><th>作者</th><th>价格</th><th>备注</th><th>日期</th><th></th><th></th>")
                .append("</tr>");
        String template = """
            <tr>
                <td align='center'>%s</td>
                <td>%s</td>
                <td align='center'>%s</td>
                <td align='center'>%s</td>
                <td>%s</td>
                <td align='center'>%s</td>
                <td align='center'><a href='./deleteBook?id=%s'>删除</a></td>
                <td align='center'><a href='./updateBook?id=%s'>修改</a></td>
            </tr>
        """;
        for (Book b : books) {
            sb.append(String.format(template,
                    b.getId(), b.getName(), b.getAuthor(), b.getPrice(), b.getMemo(), b.getPublish(),
                    b.getId(), b.getId()
            ));
        }
        sb.append("</table>");
        return sb.toString();
    }
}

